package com.chen.whereami.History;

import android.app.Activity;
import android.view.View;
import android.widget.ListView;

import com.chen.whereami.R;

public class HistorySubListView {
	private Activity mContext = null;

	private ListView mSubListView = null;

	// private LinearLayout mTitleLayout = null;

	private HistorySubDataProvider mHistorySubDataProvider = null;

	private HistorySubDataAdapter mHistorySubDataAdapter = null;

	public HistorySubListView(Activity context) {
		mContext = context;
		mHistorySubDataProvider = new HistorySubDataProvider();
		mHistorySubDataAdapter = new HistorySubDataAdapter(context);
	}

	protected void initListView() {
		if (null == mContext)
			return;
		mSubListView = (ListView) mContext.findViewById(R.id.history_lap_view);
	}

	protected void init() {
	}

	public void show(boolean show, long insertTime) {
		if (null == mSubListView)
			return;

		// if (null == mTitleLayout)
		// mTitleLayout = (LinearLayout)
		// mContext.findViewById(R.id.history_lap_title);
		//
		// if (null == mTitleLayout)
		// return;

		if (show) {
			fillDataProvider(insertTime);
			refresh();
			mSubListView.setVisibility(View.VISIBLE);
			// mTitleLayout.setVisibility(View.VISIBLE);

		} else {
			mSubListView.setVisibility(View.GONE);
			// mTitleLayout.setVisibility(View.GONE);
		}
	}

	public void fillDataProvider(long insertTime) {
		mHistorySubDataProvider.setLapList(StroageManager.getInstance()
				.getHistoryItems(insertTime));
	}

	protected void refresh() {
		if (null != mHistorySubDataAdapter)
			mHistorySubDataAdapter.notifyDataSetChanged();
		if (null != mSubListView && 0 != mSubListView.getFirstVisiblePosition()) {
			mSubListView.setSelection(0);
		}
	}

	public boolean isVisible() {
		if (null == mSubListView)
			return false;
		return mSubListView.getVisibility() == View.VISIBLE;
	}

	public void destroy(boolean rotate) {
		mContext = null;

		if (null != mHistorySubDataProvider && !rotate) {
			mHistorySubDataProvider.destroy();
			mHistorySubDataProvider = null;
		}

		if (null != mHistorySubDataAdapter) {
			mHistorySubDataAdapter.destroy();
			mHistorySubDataAdapter = null;
		}
	}

	public String getShareText() {
		return "";
	}
}
